package cn.com.surker.workcon.common;

import cn.com.surker.base.BaseEntity;
import cn.com.surker.base.system.dao.SysLineDao;
import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.workcon.service.GKTranService;
import cn.com.surker.database.JDBCTemplateCache;
import cn.com.surker.system.entity.DataSourceInfo;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author luotao
 * @date 2024/12/18
 */
public abstract class GKJDBCService implements GKTranService {

	@Resource
	protected SysLineDao sysLineDao;

	protected final String bcsCreater = "";

	//	private TransactionStatus transactionStatus;

	// protected DataSourceTransactionManager transactionManager;
	//	事务状态	key:线
	protected static Map<Long, TransactionStatus> tranStatusCache = new ConcurrentHashMap<>();
	//	事务管理器	key: 线
	private static Map<Long, DataSourceTransactionManager> tranManageCache = new ConcurrentHashMap<>();

	protected SysLine getLine(Long lineId) {
		if (lineId == null) {
			return null;
		}
		SysLine line = sysLineDao.selectById(lineId);
		if (!BaseEntity.validateExist(line)) {
			return null;
		}
		return line;
	}

	/**
	 * 获取生产线的数据连接
	 *
	 * @param line
	 * @return
	 */
	protected JdbcTemplate getLineJdbcTemp(SysLine line) {
		if (line == null) {
			return null;
		}
		DataSourceInfo lineDs = new DataSourceInfo();
		lineDs.setDriver(line.getDriver());
		lineDs.setUsername(line.getUsername());
		lineDs.setType(line.getType());
		lineDs.setPassword(line.getPassword());
		lineDs.setUrl(line.getUrl());
		JdbcTemplate jdbcTemplate = JDBCTemplateCache.getLineJdbcTemplate(line);
		if (jdbcTemplate == null) {
			//	没有连接
			return null;
		}
		//	初始化事务管理
		DataSource dataSource = jdbcTemplate.getDataSource();
		//	设置连接超时时间,单位秒
		jdbcTemplate.setQueryTimeout(JDBCTemplateCache.TIMEOUT_SECOND);
		//	查询之前是否存在过
		Long lineId = line.getId();
		DataSourceTransactionManager oldDSTranManager = tranManageCache.get(lineId);
		if (oldDSTranManager == null) {
			DataSourceTransactionManager transactionManager = JDBCTemplateCache.getTransactionManager(dataSource);
			tranManageCache.put(lineId, transactionManager);
		}
		return jdbcTemplate;
	}


	/**
	 * 开启事务
	 */
	protected boolean beginTransaction(Long lineId) {
		/*if(transactionStatus == null){
			transactionStatus = JDBCTemplateCache.beginTransaction(transactionManager);
		}*/
		TransactionStatus transactionStatus = tranStatusCache.get(lineId);
		if (transactionStatus == null) {
			//	没有开启过
			DataSourceTransactionManager transactionManager = tranManageCache.get(lineId);
			transactionStatus = JDBCTemplateCache.beginTransaction(transactionManager);
			tranStatusCache.put(lineId, transactionStatus);
			return true;
		}
		return false;
	}

	/**
	 * 提交事务
	 */
	@Override
	public void commitTranLine(Long lineId) {
		TransactionStatus transactionStatus = tranStatusCache.get(lineId);
		if (transactionStatus != null) {
			DataSourceTransactionManager transactionManager = tranManageCache.get(lineId);
			JDBCTemplateCache.commitTransaction(transactionManager, transactionStatus);
			tranStatusCache.remove(lineId);
		}
	}

	/**
	 * 回滚事务
	 */
	@Override
	public void rollbackLine(Long lineId) {
		TransactionStatus transactionStatus = tranStatusCache.get(lineId);
		if (transactionStatus != null) {
			DataSourceTransactionManager transactionManager = tranManageCache.get(lineId);
			JDBCTemplateCache.rollbackTransaction(transactionManager, transactionStatus);
			//	清空
			tranStatusCache.remove(lineId);
		}
	}

	@Override
	public void commitTranAll() {
		Set<Long> lines = tranStatusCache.keySet();
		for (Long lineId : lines) {
			TransactionStatus transactionStatus = tranStatusCache.get(lineId);
			if (transactionStatus != null) {
				DataSourceTransactionManager transactionManager = tranManageCache.get(lineId);
				JDBCTemplateCache.commitTransaction(transactionManager, transactionStatus);
				TransactionStatus remove = tranStatusCache.remove(lineId);
			}
		}
	}

	@Override
	public void rollbackAll() {
		Set<Long> lines = tranStatusCache.keySet();
		for (Long lineId : lines) {
			TransactionStatus transactionStatus = tranStatusCache.get(lineId);
			if (transactionStatus != null) {
				DataSourceTransactionManager transactionManager = tranManageCache.get(lineId);
				JDBCTemplateCache.rollbackTransaction(transactionManager, transactionStatus);
				//	清空
				tranStatusCache.remove(lineId);
			}
		}
	}
}
